package _9练习;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.Reader;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.io.file.FileReader;

@WebServlet("/ExerciseServlet")
public class ExerciseServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("text/html;charset=utf-8");
		PrintWriter pw = resp.getWriter();

		ServletContext sc = getServletContext();

		String filepath = sc.getRealPath("/WEB-INF/code.txt");
		pw.print(filepath);
	
		FileReader fileReader = new FileReader(filepath,"utf-8");
		String result = fileReader.readString();
		
		String[] strs = result.split("\n");
		for(String str:strs)
		{
			
			pw.println(str+"<br/>");
		}
		
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
}
